// 导入db模块
const db = require('../config/db');
// 数据表: db_fee_record

// 订单信息管理类
class FeeRecord {
    // 创建订单
    // 参数： parking_record_id, vehicle_num, fee_record_money,parking_record_enter_time, parking_record_leave_time
    create(parking_record_id, vehicle_num, fee_record_money, parking_record_enter_time, parking_record_leave_time) {
        return new Promise((resolve, reject) => {
            // 查询订单
            let sql = `select * from db_fee_record where parking_record_id = '${parking_record_id}'`;
            db.query(sql)
                .then(
                    (rows) => {
                        // console.log('y.2',rows);
                        console.log({parking_record_id, vehicle_num, fee_record_money, parking_record_enter_time, parking_record_leave_time})
                        if (rows.length == 0) {
                            let sql2 = `insert into db_fee_record(parking_record_id, vehicle_num, fee_record_money,parking_record_enter_time, parking_record_leave_time) values('${parking_record_id}','${vehicle_num}','${fee_record_money}','${parking_record_enter_time}','${parking_record_leave_time}')`;
                            db.query(sql2)
                                .then(
                                    (result) => {
                                        // console.log("x:",result)
                                        resolve({
                                            code: 200,
                                            msg: '创建订单成功。',
                                            parking_record_id,
                                            vehicle_num
                                        })

                                    },
                                    (err) => {
                                        console.log(err)
                                        reject({
                                            code: -1,
                                            msg: '创建订单成功失败。~~~'
                                        })
                                    }
                                )
                        }
                        else {
                            resolve({
                                code: 1,
                                msg: '该订单已经存在。'
                            })
                        }
                    },
                    (err) => {
                        reject({
                            code: -1,
                            msg: '查询订单失败。'
                        })
                    }
                )
        })
    }

    // 获取用户订单信息
    info(vehicle_num) {
        return new Promise((resolve, reject) => {
            // 查询账号
            let sql = `select *  from db_fee_record where vehicle_num = '${vehicle_num}'`;
            db.query(sql)
                .then(
                    (rows) => {
                        // console.log('y.2',rows);
                        resolve({
                            code: 200,
                            msg: '获取订单信息成功。',
                            data: rows
                        })
                    },
                    (err) => {
                        reject({
                            code: -1,
                            msg: '查询订单信息失败。'
                        })
                    }
                )
        })
    }

    // 修改订单状态
    // 参数：
    modify(parking_record_id, fee_record_status) {
        console.log(parking_record_id, fee_record_status)
        return new Promise((resolve, reject) => {
            let sql = `select * from db_fee_record where parking_record_id = '${parking_record_id}'`;
            db.query(sql)
                .then(
                    (rows) => {
                        console.log(rows)
                        if (rows.length == 1) {
                            // 修改用户资料
                            let sql2 = `update db_fee_record set fee_record_status= '${fee_record_status}' where  parking_record_id = '${parking_record_id}'`;

                            db.query(sql2)
                                .then(
                                    (result) => {
                                        // 判断是否修改成功
                                        resolve({
                                            code: 200,
                                            msg: '设置订单状态成功。',
                                            fee_record_status
                                        })

                                    },
                                    (err) => {
                                        reject({
                                            code: -1,
                                            msg: '服务端发生异常。~~~',
                                            err
                                        })
                                    }
                                )
                        }
                        else {
                            resolve({
                                code: 404,
                                msg: '该订单不存在。'
                            })
                        }
                    },
                    (err) => {
                        reject({
                            code: -1,
                            msg: '服务端发生异常。111',
                            err
                        })
                    }
                )
        })
    }

    // 全部订单信息
    list(page, pageSize) {
        return new Promise((resolve, reject) => {
            //获取分页数据
            let page_num = ((parseInt(page) - 1) * parseInt(pageSize))
            let page_size = (parseInt(pageSize))
            let sql = `select * from db_fee_record limit ${page_num}, ${page_size}`;
            let sql2 = `select count(*) as total from db_fee_record`
            // resolve({page_num, page_size})
            db.query(sql)
                .then(
                    (rows) => {
                        let data;
                        db.query(sql2)
                            .then(
                                (obj) => {
                                    data = obj
                                    resolve({
                                        code: 200,
                                        msg: '获取文章列表成功。',
                                        result: rows,
                                        page,
                                        pageSize,
                                        data
                                    })
                                }
                            )
                    },
                    err => {
                        reject({
                            code: -1,
                            msg: '获取文章列表失败。',
                            err
                        })
                    }
                )
        })
    }

    // 删除订单
    remove(parking_record_id) {
        return new Promise((resolve, reject) => {
            let sql = `select * from db_fee_record where parking_record_id = '${parking_record_id}'`
            db.query(sql)
                .then(
                    (rows) => {
                        if (rows.length == 0) {
                            resolve({
                                code: 404,
                                msg: '该订单不存在了。'
                            })
                        }
                        else {
                            // 删除
                            let sql2 = `delete from db_fee_record where parking_record_id = '${parking_record_id}'`
                            db.query(sql2)
                                .then(
                                    (result) => {
                                        // console.log("1.0",result);
                                        resolve({
                                            code: 200,
                                            msg: '删除订单记录成功。'
                                        })
                                    },
                                    () => {
                                        reject({
                                            code: -1,
                                            msg: '删除订单记录败。2222'
                                        })
                                    }
                                )
                        }
                    },
                    (err) => {
                        reject({
                            code: -1,
                            msg: '查询订单记录失败。',
                            err
                        })
                    }
                )
        })
    }

    // 搜索订单
    search(keyword) {
        return new Promise((resolve, reject) => {
            // let sql = `select * from db_fee_record where vehicle_num like '%${keyword}%'`;
            let sql = `select * from db_fee_record where vehicle_num = '${keyword}'`;
            db.query(sql)
                .then(
                    (rows) => {
                        resolve({
                            code: 200,
                            msg: `搜索的结果`,
                            result: rows
                        })
                    },
                    (err) => {
                        reject({
                            code: -1,
                            msg: '关键字搜索失败。'
                        })
                    },
                )
        })
    }
}


// 暴露订单类实例
module.exports = new FeeRecord();